from gen_py.xdcr.ttypes import *
from kazoo.client import KazooClient
from kazoo.client import KazooState

from thrift.protocol import TBinaryProtocol
from thrift.transport import TTransport

import logging

zk_host = 'cloudcomputing-zookeeper-online001-bjdxt.qiyi.virtual:2181,cloudcomputing-zookeeper-online002-bjdxt.qiyi.virtual:2181,cloudcomputing-zookeeper-online003-bjdxt.qiyi.virtual:2181,cloudcomputing-zookeeper-online004-bjdxt.qiyi.virtual:2181,cloudcomputing-zookeeper-online005-bjdxt.qiyi.virtual:2181'

TASK_NODE = '/kvservice/v1/configurations/xdc_task_infos/'
RES_NODE = '/kvservice/v1/configurations/resources/'
BACKEND_NODE = '/kvservice/v1/configurations/backend_clusters'

RED = '\033[31m'
DARK_GREEN = '\033[36m'
END = '\033[0m'


def my_listner(state):
	if state == KazooState.LOST:
		print 'lost'
	elif state == KazooState.SUSPENDED:
		print 'suspended'
	elif state == KazooState.CONNECTED:
		print 'connected'
	else:
		print 'other state'

def print_thnode(node_list):
	res = []
	for node in node_list:
		res.append(node.addr + ':' + node.port)
	return res

def print_task_info(zk):
	tasks = zk.get_children(TASK_NODE)
	print RED + '============================================= tasks info' + END
	for task in tasks:
		info = ThXdcInfo()
		value = zk.get(TASK_NODE + '/' + task)[0]
		tmb = TTransport.TMemoryBuffer(value)
		prot = TBinaryProtocol.TBinaryProtocol(tmb)
		info.read(prot)
		print '[task = ' + task + ']' + '\n\tsource_resource_name = ' + info.source_resource_name,
		print '\n\tproducer_agent_name = ' + info.producer_agent_name,
		#print '\n\tproducer_hq_nodes = ' + str(info.producer_hq_nodes),
		#print '\n\tconsumer_hq_nodes_list = ' + str(info.consumer_hq_nodes_list),
		print '\n\ttarget_resource_names = ' + str(info.target_resource_names),
		print '\n\tconsumer_agent_names = ' + str(info.consumer_agent_names),
		print '\n\thq_topic = ' + info.hq_topic
		print

def print_res_info(zk):
	resources = zk.get_children(RES_NODE)
	print RED + '============================================= resource info' + END
	for res in resources:
		resObj = ThResource()
		value = zk.get(RES_NODE + '/' + res)[0]
		tmb = TTransport.TMemoryBuffer(value)
		prot = TBinaryProtocol.TBinaryProtocol(tmb)
		resObj.read(prot)
		print '[resource = ' + res + ']' + '\n\tresource_name = ' + resObj.resource_name,
		print '\n\tdb_name = ' + resObj.db_name,
		print '\n\tdb_passwd = ' + resObj.db_passwd,
		print '\n\tauth_rdwr = ' + resObj.auth_rdwr,
		print '\n\tbackend_cluster_name = ' + resObj.backend_cluster_name,
		print '\n\tproxy_cluster_name = ' + resObj.proxy_cluster_name
		print

def print_backend_info(zk):
	backend = zk.get_children(BACKEND_NODE)
	print RED + '============================================= backend info' + END
	for b in backend:
		#print 'type',type(b),b #unicode
		obj = ThBackendCluster()
		value = zk.get(BACKEND_NODE + '/' + b)[0]
		tmb = TTransport.TMemoryBuffer(value)
		prot = TBinaryProtocol.TBinaryProtocol(tmb)
		obj.read(prot)
		print '[backend = ' + b + ']' + '\n\tname = ' + obj.cluster_name,
		print '\n\ttype = ' + ThBackendClusterType._VALUES_TO_NAMES[obj.cluster_type],
		print '\n\tnode = ' + str(print_thnode(obj.nodes))
		print
		
def add_redis_cfg(zk,node_name):
	backend = ThBackendCluster()
	backend.cluster_type = int(4)
	#backend.cluster_type = ThBackendClusterType._VALUES_TO_NAMES[3]
	backend.name = "redis.cluster_test_01"
	node_list = []
	node_list.append('10.221.48.250:19308')
	node_list.append('10.221.48.251:19308')
	backend.nodes = node_list

	tmb = TTransport.TMemoryBuffer()
	prot = TBinaryProtocol.TBinaryProtocol(tmb)
	backend.write(prot)
	zk.set(BACKEND_NODE + '/' + node_name,tmb.getvalue())

if __name__ == '__main__':
	logging.basicConfig()	
	auth = []
	auth.append(('digest','guest:Ss74073^sQ%*3Xs6'))

	zk = KazooClient(hosts=zk_host,auth_data=auth)
	zk.add_listener(my_listner)
	zk.start()

	print_res_info(zk)
	print_task_info(zk)
	#add_redis_cfg(zk,'redis.cluster_test_01')
	print_backend_info(zk)
